package com.chuang.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.chuang.server.pojo.ResultBean;
import com.chuang.server.pojo.User;
import com.chuang.server.pojo.UserLoginParams;
import com.chuang.server.service.UserService;
import javafx.scene.layout.Priority;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.Map;

@RestController
public class LoginController {
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public ResultBean login(@RequestBody UserLoginParams userLoginParams){
        logger.info(userLoginParams);
        return userService.login(userLoginParams.getUsername(),userLoginParams.getPassword());

    }
    /*
        查询用户信息
     */
    @PostMapping("/info")
    public ResultBean getInfo(@RequestBody Principal principal){
        String name = principal.getName();
        User user = userService.getUserByName(name);
        user.setLoginPassword(null);
        return ResultBean.success("获取用户信息成功",user);
    }
    /**
     * 注册
     */
    @PostMapping("/register")
    public ResultBean register(@RequestBody JSONObject jsonObject){
        return userService.register(jsonObject);
    }



}
